package com.example.paint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyView  extends View {

    public static final int RECT = 1;
    public static final int PEN = 0;
    public static final int CIRCLE = 2;

    private static final String TAG = "MyView";
    private Paint paint = new Paint();
    Path path = new Path();
    private int paintMode = 0;

    private Point rectLeftTop = new Point();
    private Point rectRightBottom = new Point();

    private Point circleCenter = new Point();
    private float radius = 0;
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        paint.setStrokeWidth(12);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
    }

    public void setPaintColr(int color) {
        paint.setColor(color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (paintMode) {
            case PEN:
                canvas.drawPath(path, paint);
                break;

            case RECT:
                canvas.drawRect(rectLeftTop.x, rectLeftTop.y, rectRightBottom.x, rectRightBottom.y, paint);
                break;

            case CIRCLE:
                canvas.drawCircle(circleCenter.x, circleCenter.y, radius, paint);
                break;
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (paintMode) {
            case PEN:
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        path.moveTo(event.getX(), event.getY());
                        break;

                    case MotionEvent.ACTION_MOVE:
                        path.lineTo(event.getX(), event.getY());
                        invalidate();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }


            case RECT:

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        rectLeftTop.x = (int) event.getX();
                        rectLeftTop.y = (int) event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        rectRightBottom.x = (int) event.getX();
                        rectRightBottom.y = (int) event.getY();
                        invalidate();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }

            case CIRCLE:

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        circleCenter.x = (int) event.getX();
                        circleCenter.y = (int) event.getY();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        radius = (float) Math.sqrt(Math.pow(event.getX()-circleCenter.x, 2) + Math.pow(event.getY() - circleCenter.y, 2));
                        invalidate();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }


        }

        return true;
    }

    public void setPaintMode(int mode) {
        paintMode = mode;
    }
}
